Page({
  data: {
    id: '',
    idcardFrontUrl: '',
    idcardBackUrl: '',
    point: '',
    building: '',
    room: '',
    name: '',
    gender: 1,
    mobile: ''

  },
  goList() {
    wx.reLaunch({
      url: '/house_pkg/pages/list/index',
    })
  },
  removePicture(ev) {
    // 移除图片的类型（身份证正面或反面）
    const type = ev.mark?.type
    this.setData({
      [type]: ''
    })
  },
  // 接收 传入参数
  onLoad({
    point,
    building,
    room,
    id
  }) {

    if (id) {
      // 编辑
      this.setData({
        id
      })
      wx.setNavigationBarTitle({
        title: '编辑房屋信息',
      })
      this.getInfo()

    } else {
      // 新增
      this.setData({
        point: decodeURIComponent(point),
        building: decodeURIComponent(building),
        room: decodeURIComponent(room)
      })
      wx.setNavigationBarTitle({
        title: '新增房屋信息',
      })
    }
  },

  // 通过id获取详情数据
  async getInfo() {
    const res = await wx.http.get('/room/' + this.data.id)
    this.setData({
      idcardFrontUrl: res.data.idcardFrontUrl,
      idcardBackUrl: res.data.idcardBackUrl,
      point: res.data.point,
      building: res.data.building,
      room: res.data.room,
      name: res.data.name,
      gender: res.data.gender,
      mobile: res.data.mobile,
    })
    console.log('详情', res);
  },




  // 表单验证
  // 姓名验证
  checkName() {
    const len = this.data.name.trim().length
    const result = len >= 2 && len < 18
    if (!result) {
      wx.utils.toast('请正确输入姓名')
    }
    return result
  },
  // 手机号验证
  checkMobile() {
    const reg = /^1[3-9]\d{9}$/
    const result = reg.test(this.data.mobile.trim())
    if (!result) {
      wx.utils.toast('请正确输入手机号')
    }
    return result
  },
  // 身份证验证
  checkCart() {
    const result = !!this.data.idcardFrontUrl && !!this.data.idcardBackUrl
    if (!result) {
      wx.utils.toast('请完整上传身份证')
    }
    return result
  },
  // 身份证上传
  // 背面
  uploadImg(e) {
    console.log(e);
    const type = e.currentTarget.dataset.type
    // 打开弹窗，选择图片
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        console.log(res.tempFiles[0].tempFilePath);
        //上传该资源 
        wx.uploadFile({
          // 临时路径
          filePath: res.tempFiles[0].tempFilePath,
          url: wx.baseURL + '/upload',
          name: 'file',
          header: {
            Authorization: getApp().token
          },
          success: (res2) => {
            console.log(res2);
            const data = JSON.parse(res2.data)
            this.setData({
              // 变量(传入的type的值): data.data.url
              [type]: data.data.url
            })
            // 人资
            // proxy:{[process.env.VUE_APP_URL]:{}}

          }
        })
      }
    })
  },



  // 提交
  async submit() {
    if (!this.checkName()) return
    if (!this.checkMobile()) return
    if (!this.checkCart()) return
    // 提交数据
    const params = {
      "point": this.data.point,
      "building": this.data.building,
      "room": this.data.room,
      "name": this.data.name,
      "gender": this.data.gender,
      "mobile": this.data.mobile,
      "idcardFrontUrl": this.data.idcardFrontUrl,
      "idcardBackUrl": this.data.idcardBackUrl
    }
    if (this.data.id) {
      // 编辑
      params.id = this.data.id
    }
    await wx.http({
      url: '/room',
      method: 'post',
      data: params
    })
    wx.showToast({
      title: this.data.id ? '编辑房屋成功' : '添加房屋成功',
    })
    // 关闭当前页面再跳转到列表页面
    // wx.redirectTo({
    //   url: '../list/index',
    // })
    wx.reLaunch({
      url: '../list/index',
    })

  }
})